{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "追加[中原地产](http://sh.centanet.com/chengjiao/contrast/?cestcode=&regionid=0&gscopeid=0)相关信息\n",
    "* 全市均价\n",
    "* 全市均价环比上月\n",
    "* 全市均价同比去年\n",
    "* 全市在售房源\n",
    "* 全市近60天成交量\n",
    "* 以上信息浦东，嘉定，奉贤"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "URLS\n",
    "* 全市：http://sh.centanet.com/chengjiao/contrast/?cestcode=&regionid=0&gscopeid=0\n",
    "* 浦东：http://sh.centanet.com/chengjiao/contrast/?cestcode=&regionid=2172&gscopeid=0\n",
    "* 浦东川沙：http://sh.centanet.com/chengjiao/contrast/?cestcode=&regionid=2172&gscopeid=217218\n",
    "* 嘉定：http://sh.centanet.com/chengjiao/contrast/?cestcode=&regionid=2178&gscopeid=0\n",
    "* 奉贤：http://sh.centanet.com/chengjiao/contrast/?cestcode=&regionid=2180&gscopeid=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import requests,time,os,csv,re,logging\n",
    "from bs4 import BeautifulSoup\n",
    "import numpy as np\n",
    "import traceback\n",
    "\n",
    "from lianjia_requests import get_n_times"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ex_date = time.strftime('%Y%m%d')\n",
    "ex_time = time.strftime('%H:%M:%S')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(ex_date + ex_time+ \"：正在获取...\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "urls_dict={\"lianjia\":\"http://sh.lianjia.com/ershoufang\",\n",
    "           \"Shanghai\":\"http://sh.centanet.com/chengjiao/contrast/?cestcode=&regionid=0&gscopeid=0\",\n",
    "           \"pudong\":\"http://sh.centanet.com/chengjiao/contrast/?cestcode=&regionid=2172&gscopeid=0\",\n",
    "           \"pudong_chuansha\":\"http://sh.centanet.com/chengjiao/contrast/?cestcode=&regionid=2172&gscopeid=217218\",\n",
    "           \"jiading\":\"http://sh.centanet.com/chengjiao/contrast/?cestcode=&regionid=2178&gscopeid=0\",\n",
    "           \"fengxian\":\"http://sh.centanet.com/chengjiao/contrast/?cestcode=&regionid=2180&gscopeid=0\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "fieldnames=['ex_date','ex_time', \n",
    "            'lj_on_sale','lj_90day_deal', 'lj_yesterday_viewed',\n",
    "           'sh_on_sale','sh_60day_deal','sh_ave_p','sh_huanbi','sh_tongbi',\n",
    "           'pd_on_sale','sh_60day_deal','sh_ave_p','sh_huanbi','sh_tongbi']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "url = 'http://sh.lianjia.com/ershoufang'\n",
    "html =get_n_times(url, n=6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "soup=BeautifulSoup(html,'html.parser')\n",
    "spans=soup.find_all('span',attrs={'class':'num strong-num'})\n",
    "\n",
    "total_on_sale=spans[0].string\n",
    "last_90day_deal = spans[1].string\n",
    "yesterday_viewed = spans[2].string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "item={'ex_date':ex_date,\n",
    "      'ex_time':ex_time, \n",
    "      'total_on_sale':total_on_sale,\n",
    "      'last_90day_deal':last_90day_deal,\n",
    "      'yesterday_viewed':yesterday_viewed}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "path=os.path.join(os.getcwd(),'results/lianjia_trends.csv')\n",
    "\n",
    "fieldnames=['ex_date','ex_time', 'total_on_sale','last_90day_deal', 'yesterday_viewed']\n",
    "\n",
    "if  not os.path.exists(path):     #如果文件不存在，创建文件并写入表头\n",
    "        csv_file=open(path,'w',newline='')\n",
    "        writer=csv.DictWriter(csv_file,fieldnames=fieldnames)\n",
    "        writer.writeheader()\n",
    "        csv_file.close()\n",
    "        \n",
    "csv_file=open(path,'a+',newline='')\n",
    "writer=csv.DictWriter(csv_file,fieldnames=fieldnames)\n",
    "writer.writerow(item)\n",
    "csv_file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "获取结束，3秒后关闭\n"
     ]
    }
   ],
   "source": [
    "n=3\n",
    "print('获取结束，%d秒后关闭' %n)  \n",
    "time.sleep(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
